package com.auth.Utils;

import cn.hutool.core.util.StrUtil;
import com.auth.constant.CacheConstants;
import com.auth.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户信息服务
 *
 * @author jwb
 */
@Component
public class LoginUserHelper {

    @Autowired
    private RedisService redisService;

    /**
     * 获取request中当前登录的用户name
     */
    public static String getLoginUserName() {
        return ServletUtils.getRequest().getHeader(CacheConstants.USER_NAME);
    }

    /**
     * 获取request当前登录的用户id
     */
    public static Long getLoginUserId() {
        String userId = ServletUtils.getRequest().getHeader(CacheConstants.USER_ID);
        if (userId != null) {
            return Long.parseLong(userId);
        }
        return null;
    }

    /**
     * 获取request当前登录的用户终端
     */
    public static String getLoginUserClient() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CLIENT);
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 获取请求中token
     */
    public static String getToken() {
        return getToken(ServletUtils.getRequest());
    }

    /**
     * 根据request获取请求中token
     */
    public static String getToken(HttpServletRequest request) {
        String token = request.getHeader(CacheConstants.AUTHORIZATION_HEADER);
        return token;
    }

    public static String getTokenCachKey(String token) {
        return CacheConstants.USER_TOKEN_KEY + token;
    }

    public static String getUserCachKey(String client, Long userId) {
        return CacheConstants.USER_KEY + client + ":" + userId;
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUser getLoginUser() {
        return getLoginUser(ServletUtils.getRequest());
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUser getLoginUser(HttpServletRequest request) {
        // 获取请求携带的令牌
        String token = LoginUserHelper.getToken(request);
        return getLoginUser(token);
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUser getLoginUser(String token) {
        if (StrUtil.isBlank(token)) {
            return null;
        }
        String userKey = LoginUserHelper.getTokenCachKey(token);
        return redisService.getCacheObject(userKey);
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUser getLoginUser(String client, Long userId) {
        // 获取请求携带的令牌
        String token = redisService.getCacheObject(LoginUserHelper.getUserCachKey(client, userId));
        return getLoginUser(token);
    }

    /**
     * 更新用户身份信息
     */
    public void updateLoginUser(LoginUser loginUser) {
        if (loginUser != null && StrUtil.isNotEmpty(loginUser.getToken())) {
            // 覆盖缓存
            redisService.setCacheObject(LoginUserHelper.getTokenCachKey(loginUser.getToken()), loginUser);
        }
    }

    /**
     * 删除用户缓存记录
     */
    public boolean delLoginUser() {
        // 获取请求携带的令牌
        String token = LoginUserHelper.getToken(ServletUtils.getRequest());
        return delLoginUser(token);
    }

    /**
     * 删除用户缓存记录
     * @param token
     */
    public boolean delLoginUser(String token) {
        if (StrUtil.isNotBlank(token)) {
            return redisService.deleteObject(LoginUserHelper.getTokenCachKey(token));
        }
        return false;
    }

}